Workingwiththeaddcommand
Let'sworkwiththeaddcommand,forexample,forparsingyourargumentsand
calling the functions. Once the add command gets called, we want to call a
functiondefinedinnotes,whichwillberesponsibleforactuallyaddingthenote.
Thenotes.addNotefunctionwillgetthejobdone.Now,whatdowewanttopassto
theaddNotefunction?Wewanttopassintwothings:thetitle,whichisaccessible
onargv.title,aswesawintheprecedingexample;andthebody,argv.body:
console.log('Startingapp.js');
constfs=require('fs');
const_=require('lodash');
constyargs=require('yargs');
constnotes=require('./notes.js');
constargv=yargs.argv;
varcommand=process.argv[2];
console.log('Command:',command);
console.log('Process',process.argv);
console.log('Yargs',argv);
if(command==='add'){
console.log('Addingnewnote');
notes.addNote(argv.title,argv.body);
}elseif(command==='list'){
console.log('Listingallnotes');
}elseif(command==='read'){
console.log('Readingnote');
}elseif(command==='remove'){
console.log('Removingnote');
}else{
console.log('Commandnotrecognized');
}
Currently, these command-line arguments, title and body, aren't
required.Sotechnically,theusercouldruntheapplicationwithout
oneofthem,whichwouldcauseittocrash,butinfuture,we'llbe
requiringbothofthese.
Nowthatwehavenotes.addNoteinplace,wecanremoveourconsole.logstatement,
which was just a placeholder, and we can move into the notes application
notes.js.
Insidenotes.js,we'llgetstartedbymakingavariablewiththesamenameasthe
methodweusedoverapp.jsandaddNote,andwewillsetitequaltoananonymous